using ECatalog.Controllers;
using ECatalog.Models;
using ECatalog.Models.Admin;
using ECatalog.Models.Colors;
using ECatalog.Models.Kits;
using ECatalog.Models.Locations;
using ECatalog.Models.Mailer;
using ECatalog.Models.ModelSizes;
using ECatalog.Models.Models;
using ECatalog.Models.Organizations;
using ECatalog.Models.Shops;
using ECatalog.Models.Structures;
using ECatalog.Models.UserIU;
using ECatalog.Models.UserModel;

[assembly: WebActivator.PreApplicationStartMethod(typeof(ECatalog.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(ECatalog.App_Start.NinjectWebCommon), "Stop")]

namespace ECatalog.App_Start
{
    using System;
    using System.Web;

    using Microsoft.Web.Infrastructure.DynamicModuleHelper;

    using Ninject;
    using Ninject.Web.Common;

    public static class NinjectWebCommon
    {
        private static readonly Bootstrapper bootstrapper = new Bootstrapper();

        /// <summary>
        /// Starts the application
        /// </summary>
        public static void Start()
        {
            DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
            DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
            bootstrapper.Initialize(CreateKernel);
        }

        /// <summary>
        /// Stops the application.
        /// </summary>
        public static void Stop()
        {
            bootstrapper.ShutDown();
        }

        /// <summary>
        /// Creates the kernel that will manage your application.
        /// </summary>
        /// <returns>The created kernel.</returns>
        private static IKernel CreateKernel()
        {
            var kernel = new StandardKernel();
            kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
            kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

            RegisterServices(kernel);
            return kernel;
        }

        /// <summary>
        /// Load your modules or register your services here!
        /// </summary>
        /// <param name="kernel">The kernel.</param>
        private static void RegisterServices(IKernel kernel)
        {
            kernel.Bind<IUserRepository>().To<UserRepository>();
            kernel.Bind<IModelRepository>().To<ModelRepository>();
            kernel.Bind<IModelBuilder>().To<ModelBuilder>();
            kernel.Bind<IOrganizationRepository>().To<OrganizationRepository>();
            kernel.Bind<IOrganizationBuilder>().To<OrganizationBuilder>();
            kernel.Bind<IStructureRepository>().To<StructureRepository>();
            kernel.Bind<IShopRepository>().To<ShopRepository>();
            kernel.Bind<IKitRepository>().To<KitRepository>();
            kernel.Bind<IKitBuilder>().To<KitBuilder>();
            kernel.Bind<IColorRepository>().To<ColorRepository>();
            kernel.Bind<ILocationRepository>().To<LocationRepository>();
            kernel.Bind<IShopBuilder>().To<ShopBuilder>();
            kernel.Bind<IBuilder>().To<Builder>();
            kernel.Bind<IAdminBuilder>().To<AdminBuilder>();
            kernel.Bind<IAdminRepository>().To<AdminRepository>();
            kernel.Bind<IMailer>().To<Mailer>();
            kernel.Bind<IModelSizeRepository>().To<ModelSizeRepository>();
        }
    }
}
